package com.base.common.service;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.base.common.persistence.Dao;
import com.base.common.persistence.Model;
import com.base.common.persistence.Page;
import com.base.common.persistence.utils.SqlMapper;

public abstract class DBManageService<D extends Dao<T>, T extends Model<T>> {
	@Autowired
	SqlMapper sqlMapper;
	
	public SqlMapper getSqlMapper() {
		return sqlMapper;
	}
	
	
	public Page<T> getPage(Page<T> page, String sql, Class<T> a, Map<String, String> params) {
		int pageNo = page.getPageNo();
		int pageSize = page.getPageSize();
		int startIndex = (pageNo - 1) * pageSize;
		//int endIndex = pageNo * pageSize - 1;
		page.setRecords(this.getSqlMapper().selectList(sql, params).size());
		sql += " LIMIT " + startIndex + "," + pageSize;
		List<T> list = this.getSqlMapper().selectList(sql, params, a);
		page.setRows(list);
		return page;
	}
}
